# TODO: 处理excel文件
import numbers
import pandas as pd
import numpy as np


def generate_behaviour_list(data_list):
    behavior_list = []
    for i in range(len(data_list)):
        behavior_list.append(data_list[i][0])
    return behavior_list


def generate_lsa_data(data_list):
    lsa_data = []
    for i in range(len(data_list)):
        lsa_data.append(data_list[i][1:])
    return lsa_data


def generate_render_list(behavior_list: list[str], lsa_list: list[list[numbers]]):
    """
    每一项元素的格式为
    { "A": "XX", "B": "XX", "C":"XXX" }
    """
    render_list = []
    for i in range(len(lsa_list)):
        render_row = {}
        for j in range(len(lsa_list[i])):
            render_row[behavior_list[j]] = lsa_list[i][j]
        render_row["behaviors"] = behavior_list[i]
        render_list.append(render_row)
    return render_list


def generate_lsa_list_for_render(behavior_list: list[str], lsa_data: list[list[numbers]]):
    # DONE: 生成前端拿来画图渲染的lsa数据
    # 1. vis用于绘制的nodes
    nodes = []
    for i in range(len(lsa_data)):
        nodes.append({"id": i, "label": behavior_list[i], "size": 10})
    # 2. vis用于绘制的edges
    edges = []
    for i in range(len(lsa_data)):
        for j in range(len(lsa_data[i])):
            if lsa_data[i][j] > 1.96:
                # label保留两位数据
                edges.append({"from": i, "to": j, "label": str(round(lsa_data[i][j], 2))})
    return {"nodes": nodes, "edges": edges}


def read_excel(excel_file):
    df = pd.read_excel(excel_file)
    data_list = np.array(df).tolist()
    # 生成行为序列列表
    behavior_list = generate_behaviour_list(data_list)
    # 生成lsa二维数组列表
    lsa_data = generate_lsa_data(data_list)
    # 生成render_list
    render_list = generate_render_list(behavior_list, lsa_data)
    # 生成vis数据
    lsa_list_for_render = generate_lsa_list_for_render(behavior_list, lsa_data)
    return {"behaviorList": behavior_list, "lsaData": lsa_data, "renderList": render_list, "nodes": lsa_list_for_render["nodes"], "edges": lsa_list_for_render["edges"]}
